package com.inno.auth.controller.controller;


import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.google.common.base.Preconditions;
import com.inno.auth.common.annotiation.Log;
import com.inno.auth.common.entity.CommonReturnType;
import com.inno.auth.common.enums.LoginTypeEnum;
import com.inno.auth.common.utils.DesensitizedUtil;
import com.inno.auth.controller.dto.PlannerUsersDTO;
import com.inno.auth.domain.entity.PlannerUsersBO;
import com.inno.auth.domain.service.UsersDomainService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sean
 */
@RestController
@RequestMapping("/auth/")
@RequiredArgsConstructor
public class LoginController {
    private final UsersDomainService usersDomainService;



    @PostMapping("/login")
    @Log(operationType = "账号密码登录")
    public CommonReturnType<SaResult> login(@RequestBody PlannerUsersDTO usersDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(usersDTO.getUsername()),"用户名不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(usersDTO.getPassword()),"密码不能为空");
        usersDTO.setType(LoginTypeEnum.LOGIN_TYPE_PASSWORD);
        PlannerUsersBO plannerUsersBO = convertDTOToBO(usersDTO);


        return CommonReturnType.success( login(plannerUsersBO));
    }

    private SaResult login(PlannerUsersBO plannerUsersBO) {
        PlannerUsersBO boResult = this.usersDomainService.login(plannerUsersBO);
        PlannerUsersDTO plannerUsersDTO = convertBOToDTO(boResult);
        plannerUsersDTO.setPassword(DesensitizedUtil.password(plannerUsersDTO.getPassword()));
        StpUtil.login(plannerUsersDTO.getId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return SaResult.ok().setData(tokenInfo);

    }

    /**
     * 手机号登录
     */
    @PostMapping("/mobile-login")
    @Log(operationType = "手机号登录")
    public CommonReturnType<SaResult> mobileLogin(@RequestBody PlannerUsersDTO usersDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(usersDTO.getMobile()),"手机号不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(usersDTO.getCode()),"请输入验证码");
        usersDTO.setType(LoginTypeEnum.LOGIN_TYPE_SHORT_MESSAGE);
        PlannerUsersBO plannerUsersBO = convertDTOToBO(usersDTO);
        return CommonReturnType.success(this.login(plannerUsersBO));
    }

    /**
     * 小程序
     * 检查是否已注册过
     */
    @PostMapping("/check")
    public CommonReturnType<SaResult> check(@RequestBody PlannerUsersDTO usersDTO) {
        SaResult result = this.usersDomainService.check(usersDTO.getWechatCode());

        return CommonReturnType.success(result);
    }
    /**
     * 小程序登录
     */
    @PostMapping("/mp-login")
    @Log(operationType = "小程序登录")
    public CommonReturnType<SaResult> mpLogin(@RequestBody PlannerUsersDTO plannerUsersDTO){
        Preconditions.checkArgument(!StringUtils.isBlank(plannerUsersDTO.getWechatCode()),"code值为空");
        plannerUsersDTO.setType(LoginTypeEnum.LOGIN_TYPE_WECHAT);
        PlannerUsersBO plannerUsersBO = convertDTOToBO(plannerUsersDTO);
        return CommonReturnType.success(login(plannerUsersBO));
    }
    @PostMapping("/adminLogin")
    @Log(operationType = "管理员登录")
    public CommonReturnType<SaResult> adminLogin(@RequestBody PlannerUsersDTO usersDTO){
        PlannerUsersBO plannerUsersBO = convertDTOToBO(usersDTO);
        PlannerUsersBO boResult = this.usersDomainService.adminLogin(plannerUsersBO);
        PlannerUsersDTO plannerUsersDTO = convertBOToDTO(boResult);
        plannerUsersDTO.setPassword(DesensitizedUtil.password(plannerUsersDTO.getPassword()));
        StpUtil.login(plannerUsersDTO.getId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return CommonReturnType.success(SaResult.ok().setData(tokenInfo));
    }

    @PostMapping("/send-register-sms")
    @Log(operationType = "发送登录/注册验证码")
    public CommonReturnType<Void> sendRegisterSms(@RequestBody PlannerUsersDTO usersDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(usersDTO.getMobile()),"手机号不能为空");
        this.usersDomainService.sendRegisterSms(convertDTOToBO(usersDTO));
        return CommonReturnType.success();
    }

    @PostMapping("/register")
    @Log(operationType = "注册")
    public CommonReturnType<Void> register(@RequestBody PlannerUsersDTO usersDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(usersDTO.getMobile()),"手机号不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(usersDTO.getPassword()),"请设置密码");
        Preconditions.checkArgument(!StringUtils.isBlank(usersDTO.getCode()),"请输入验证码");

        PlannerUsersBO plannerUsersBO = convertDTOToBO(usersDTO);
        this.usersDomainService.register(plannerUsersBO);

        return CommonReturnType.success();

    }

    private PlannerUsersBO convertDTOToBO(PlannerUsersDTO usersDTO) {
        PlannerUsersBO plannerUsersBO = new PlannerUsersBO();
        BeanUtil.copyProperties(usersDTO, plannerUsersBO);
        return plannerUsersBO;
    }

    private PlannerUsersDTO convertBOToDTO(PlannerUsersBO boResult) {
        PlannerUsersDTO plannerUsersDTO = new PlannerUsersDTO();
        BeanUtil.copyProperties(boResult, plannerUsersDTO);
        return plannerUsersDTO;
    }
}
